/**
 * 通过云服务获取资源
 */

const cloudPath = '/container-trashrecycle-core'
// const cloutFilePath = 'cloud://trashrecycle-core-4e1w8p237dea1b.7472-trashrecycle-core-4e1w8p237dea1b-1305867727/'
const cloutFilePath = 'cloud://trashrecycle-core-0eiz3s5fc13519.7472-trashrecycle-core-0eiz3s5fc13519-1306034317/'

const getOpenId = (code)=>{
  return  wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/user/login?code=${code}&tag=0`
  })
}
const addUser = (nickname,userId,imageUrl,integration,openId,orderNum,sessionKey,trashWeight)=>
{ 
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/user/add`,
    data:{
      imageUrl,
      integration,
      nickname,
      openId,orderNum,sessionKey,
      trashWeight,
      userId
    },
    method:'POST'
  })
}
const getUserById = (openid)=>{
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/user/sample?id=${openid}`
  })
}
const userInterface = (imageUrl,integration,nickname,openId,orderNum,sessionKey,trashWeight,userId,type)=>{

    let url,method
    switch(type)
    {
      case 'add':
        url="/api/sys/core/user/add",
        method='POST'
        break;
      case 'update':
        url="/api/sys/core/user/saveorupdate"
        method='POST'
        break
    }
    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        imageUrl,
        integration,
        nickname,
        openId
        ,orderNum,sessionKey,
        trashWeight,
        userId      
      },
      method:method
    })
}


const addEasy = (blogId,userId,content,imageUrl,comment,thumb,time)=>{
        return wx.cloud.callContainer({
        path:cloudPath+'/api/sys/core/user/add',
        method:"POST",
        data:{
          blogId,
          comment,
          content,
          imageUrl,
          thumb,
          time,
          title,
          userId
        }
})
}

const AppointOrderInterface  = (addrNo=null,courierName=null,courierTel=null,integration=null,isClassified,items,orderId,orderStatus,senderAddress,senderDateBegin,senderDateEnd,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId,type)=>
{

    let url,method
    switch(type)
    {
      case 'add':
        method="POST"
        url="/api/sys/core/appointorder/add"
        break;
      case 'getAll':
        method="POST"
        url="/api/sys/core/appointorder/filter/list"
        break;
      case 'getByOrderId':
        method="GET"
        url=`/api/sys/core/appointorder/sample?id=${orderId}`
        break;
    }

    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        addrNo,courierName,courierTel,integration,isClassified,items,orderId,orderStatus,senderAddress,senderDateBegin,senderDateEnd,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId  
      },
      method:method
    })
}

  const getAllAppointOrder = (category,courierName,courierTel,integration,orderId,orderStatus,senderAddress,senderDate,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId)=>
  {
    return wx.cloud.callContainer({
      path:cloudPath+'/api/sys/core/appointorder/list',
      data:{
        category,courierName,courierTel,integration,orderId,orderStatus,senderAddress,senderDate,senderName,senderRemark,senderTel,senderUrgent,senderWeight,time,userId
      },
      method:'POST'
    })
 }

    const deliverorderInterface = (addrNo,category,feedback,integration,orderId,orderStatus,place,time,userId,weight,type,bucketId=null) =>
    {
        var url,method;
        switch (type) {
          case 'add':         
            url="/api/sys/core/deliverorder/add"
            method='POST'
            break;
          case 'getAll':
            url="/api/sys/core/deliverorder/filter/list"
            method='POST'
            break;
          case 'getByOrderId':
            url=`/api/sys/core/deliverorder/sample?id=${orderId}`
            method='GET'
            break
        }
        return wx.cloud.callContainer({
          path:cloudPath+url,
          data:{
            addrNo,bucketId,category,feedback,integration,orderId,orderStatus,place,time,userId,weight
          },
          method:method
        })
        
  }

  const deliverOrderPageList = (pageNo,userId)=>{
    return wx.cloud.callContainer({
      path:cloudPath+`/api/sys/core/deliverorder/pageList?pageNo=${pageNo}&userId=${userId}`,
    })
  }

  const appointOrderPageList = (pageNo,userId)=>{
    return wx.cloud.callContainer({
      path:cloudPath+`/api/sys/core/appointorder/pageList?pageNo=${pageNo}&userId=${userId}`,
    })
  }

  const communityInterface  =(blogId,comment,content,imageUrl,thumb,time,title,userId,type)=>
  {

      var url,method;
      switch (type) {
        case 'add':         
        url="/api/sys/core/community/add"
        method='POST'
        break;
      case 'getAll':
        url="/api/sys/core/community/filter/list"
        method='POST'
        break;
      case 'update':
        url="/api/sys/core/community/saveorupdate"
        method='POST'
        break;
      }
      return wx.cloud.callContainer({
        path:cloudPath+url,
        data:{
          blogId,comment,content,imageUrl,thumb,time,title,userId
        },
        method:method
      })
  }

//   const uploadImg = function(filePath){
//     // debugger
//     console.log('上传图片')
//     console.log(filePath)

//     let timestamp = (new Date()).valueOf();

//        // 将图片上传至云存储空间
//       return wx.cloud.uploadFile({
//         // 指定上传到的云路径
//         cloudPath: cloutFilePath+'Eassyimgs/'+ timestamp + '.png',
//         // 指定要上传的文件的小程序临时文件路径
//         filePath: filePath,
//         // 成功回调
//         success: res => {
//          console.log('上传成功', res)
 
//         //  if (res.fileID) {
//         //   that.setData({
//         //    zhaopian: '图片如下',
//         //    imgUrl: res.fileID
//         //   })
//         //  }
//         }
//       })
//     // return new Promise((resolve,reject)=>{
//         // return  wx.uploadFile({
//         //     filePath: filePath,
//         //     name: 'file',
//         //     url:cloudPath+'/api/sys/core/community/image/upload',
//         //     formData: {
//         //       'user':"app.globalData.userInfo.openId"
//         //     },
//         //     success: (result) => {
//         //       console.log(result)
//         //       resolve(JSON.parse(result.data).data.value)
//         //     },
//         //     fail: (res) => {
//         //       console.log(res)
//         //     },
//         //     complete: (res) => {
//         //       console.log(res)
//         //     },
//         //   })


//     // })

// }

const downloadFile=function(path)
{
  return new Promise((resolve,reject)=>{
    wx.cloud.downloadFile({
      fileID: cloutFilePath+path,
      success: res => {
        resolve(res.tempFilePath)
      },
      fail: err => {
        // handle error
        console.log(err)
      }
    })
  })

}

const thumbInterface = (blogId,userId,nickname="123",type)=>{
    let url,method
    switch(type)
    {
      case 'getIsThumb':
        url="/api/sys/core/thumb/filter/sample"
        method='POST'
        break;
      case 'addThumb':
        url="/api/sys/core/thumb/add"
        method='POST'
        break;
    }
    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        blogId,userId,nickname
            },
            method:method
    })
}

const deleteThumb = (id)=>{
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/thumb/delete?id=${id}`,
    method:'DELETE'
  })
}
const commentInterface = (commentId,blogId,userId,content,nickname,isChecked,commentUserId,type)=>{
    let url,method
    switch(type)
    {
      case 'getCommentListByblogId':
        url="/api/sys/core/comment/filter/list"
        method='POST'
        break;
      case 'addComment':
        url="/api/sys/core/comment/add"
        method='POST'
        break;
    }
    
    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        commentId,blogId,userId,content,nickname,isChecked,commentUserId
      },
      method:method
    })
}

const noticeUserInterface = (id,isChecked,systemNoticeId,userId,type)=>{
    let url,        method
    switch(type)
    {
      case 'getAllNotice':
        url="/api/sys/core/notice-user/filter/list"
        method='POST'
        break;
      case 'add':
        url="/api/sys/core/notice-user/add"
        method='POST'
        break;
    }
    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        id,isChecked,systemNoticeId,userId
            },
            method:method
    })

}
const noticeInterface = (systemNoticeId,content,type)=>{
    let url,method
    switch(type)
    {
      case 'add':
        url="/api/sys/core/notice/add"
        method='POST'
        break;
      case 'getNoticeContent':
        url=`/api/sys/core/notice/sample?id=${systemNoticeId}`
        method='GET'
    }

    return wx.cloud.callContainer({
      path:cloudPath+url,
      data:{
        id:systemNoticeId,content
                  },
                  method:method
    })

}
const getimgByBlogId = (blogId)=>{
  
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/community/image/url?id=${blogId}`,
  })
}

const addressInterface = (address,addressId,name,telphone,userId,isdefault,type)=>{
  let url,method
  switch(type)
  {
    case 'add':
      url="/api/sys/core/address/add"
      method='POST'
      break;
    case 'delete':
      url="/api/sys/core/address/delete"
      method='DELETE'
      break;
    case 'getAll':
      url="/api/sys/core/address/filter/list"
      method='POST'
      break;
    case 'saveOrUpdate':
      url="/api/sys/core/address/saveorupdate"
      method='POST'
      break;
    case 'getById':
      url=`/api/sys/core/address/sample?id=${addressId}`
      method='GET'
      break;
  }
  return wx.cloud.callContainer({
    path:cloudPath+url,
    data:{
      address,addressId,name,telphone,userId,isdefault
          },
    method:method
  })
}

const getAddressByOrder = (userId)=>{
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/address/ex/list?userId=${userId}`,
    // data:{userId},
    method:'POST'
  })
}
const getEassysByPage = (pageNo)=>{
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/community/pageList?pageNo=${pageNo}`,
    method:'GET'
  })
}
const getUserTopList = ()=>{
  return wx.cloud.callContainer({
    path:cloudPath+`/api/sys/core/user/rankList?pageNo=1`,
    method:'GET'
  })
}

const dailyCheckInterface = (clock,userId,type)=>{
  let url,method
  switch(type)
  {
    case 'add':
      url="/api/sys/core/clock/add"
      method='POST'
      break;
    case 'getAll':
      url="/api/sys/core/clock/filter/list"
      method='POST'
      break;
  }
  return wx.cloud.callContainer({
    path:cloudPath+url,
    data:{
      clock,userId
          },
    method:method
  })
}

const storeInterface = (addressId,goodsId,num,orderId,price,status,time,userId,type)=>{
  let url,method
  switch(type)
  {
    case 'add':
      url="/api/sys/core/order/ex/add"
      method='POST'
      break;
    case 'getByOrderId':
      url=`/api/sys/core/order/ex/list?orderId=${orderId}`
      method='POST'
      break;
    case 'getByUserId':
      url=`/api/sys/core/order/ex/listid?userId=${userId}`
      method='POST'
      break;
  }
  return wx.cloud.callContainer({
    path:cloudPath+url,
    data:{
      addressId,goodsId,num,orderId,price,status,time,userId
          },
    method:method
  })
}

const goodsInterface = (des,detail,goodsId,image,price,thumb,title,type)=>{
  let url,method
  switch(type)
  {
    case 'add':
      url="/api/sys/core/goods/add"
      method='POST'
      break;
    case 'getAll':
      url="/api/sys/core/goods/filter/list"
      method='POST'
      break;
    case 'getById':
      url=`/api/sys/core/goods/sample?id=${goodsId}`
      method='GET'
      break;
  }
  return wx.cloud.callContainer({
    path:cloudPath+url,
    data:{
      des,detail,goodsId,image,price,thumb,title
         },
    method:method
  })
}

const getFileFromCloud = (fileId)=>{
  wx.cloud.downloadFile({
    fileID: fileId,
    success: res => {
      // get temp file path
      // console.log('tempFilePath',res.tempFilePath)
      return res.tempFilePath
    },
    fail: err => {
      // handle error
      console.log(err)
    }
  })
}

const bucketInterface = (bucketId,type,weight=null,addrNo=null,category=null,isFull=null,latitude=null,longtitude=null,maxWeight=null,place=null)=>{
  let url,method
  switch(type)
  {
    case 'getAll':
      url="/api/sys/core/bucket/filter/list"
      method='POST'
      break;
    case 'getById':
      url=`/api/sys/core/bucket/sample?id=${bucketId}`
      method='GET'
      break;
    case 'update':
      url=`/api/sys/core/bucket/saveorupdate`
      method='POST'
      break
  }
  return wx.cloud.callContainer({
    path:cloudPath+url,
    data:{
      bucketId,weight,addrNo,category,isFull,latitude,longtitude,maxWeight,place
         },
    method:method
  })
}


module.exports = {
  addUser,
  getUserById,
  userInterface,
  addEasy,
  AppointOrderInterface,
  getAllAppointOrder,
  deliverorderInterface,
  communityInterface,
  // uploadImg,
  downloadFile,
  getOpenId,
  thumbInterface,
  deleteThumb,
  commentInterface,
  noticeUserInterface,
  noticeInterface,
  getimgByBlogId,
  addressInterface,
  deliverOrderPageList,
  appointOrderPageList,
  getEassysByPage,
  getUserTopList,
  dailyCheckInterface,
  getAddressByOrder,
  storeInterface,
  goodsInterface,
  getFileFromCloud,
  bucketInterface
}